﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DTO;
using DAO;

namespace BUS
{
    public class Sach_BUS
    {
        private static Sach_BUS _instance = null;

        public static Sach_BUS SACH
        {
            get
            {
                if (_instance == null)
                    _instance = new Sach_BUS();

                return _instance;
            }
        }

        public void ThemSach(Sach_DTO sach)
        {
            Sach_DAO.SACH.ThemSach(sach);
        }

        public void SuaSach(Sach_DTO sach)
        {
            Sach_DAO.SACH.SuaSach(sach);
        }

        public void XoaSach(int ma)
        {
            Sach_DAO.SACH.XoaSach(ma);
        }

        public Sach_DTO LaySach(int ma)
        {
            try
            {
                List<Sach_DTO> result = new List<Sach_DTO>();
                DataTable dt = Sach_DAO.SACH.LaySach(ma);
                DataRowCollection list = dt.Rows;
                if (list.Count == 0)
                    return null;

                DataRow dr = list[0];

                int a = (int)dr["MaSach"];
                int b = (int)dr["SoLuongTon"];

                return new Sach_DTO((int)dr["MaSach"], (string)dr["TenSach"], (string)dr["TheLoai"], (string)dr["TacGia"], (int)dr["SoLuongTon"]);
            }
            catch
            {
                return null;
            }
        }

        public DataTable LayToanBoSach()
        {
            return Sach_DAO.SACH.LayToanBoSach();
        }

        public DataTable TimSachTheoMa(int text)
        {
            return Sach_DAO.SACH.TimSachTheoMa(text);
        }

        public DataTable TimSachTheoTen(string tensach)
        {
            return Sach_DAO.SACH.TimSachTheoTen(tensach);
        }

        public DataTable TimSachTheoTheLoai(string text)
        {
            return Sach_DAO.SACH.TimSachTheoTheLoai(text);
        }

        public DataTable TimSachTheoTacGia(string text)
        {
            return Sach_DAO.SACH.TimSachTheoTacGia(text);
        }

        public DataTable TimTheoKhachHang(string ten)
        {
            return Sach_DAO.SACH.TimTheoKhachHang(ten);
        }

        public DataTable TimSachTheoTatCa(Sach_DTO sach)
        {
            return Sach_DAO.SACH.TimSachTheoTatCa(sach);
        }

        public DataTable TimSachTheoSoLuongTon(int tu, int den)
        {
            return Sach_DAO.SACH.TimSachTheoSoLuongTon(tu, den);
        }
    }

    public class ThamSo_BUS
    {
        private static ThamSo_BUS _thamSo = null;

        public static ThamSo_BUS THAMSO
        {
            get
            {
                if (_thamSo == null)
                    _thamSo = new ThamSo_BUS();

                return _thamSo;
            }
        }

        public bool SuaThamSo(ThamSo_DTO t)
        {
            return ThamSo_DAO.THAMSO.SuaThamSo(t);
        }

        public int LaySLNhapToiThieu()
        {
            return ThamSo_DAO.THAMSO.LaySLNhapToiThieu();
        }

        public int LaySLTonToiThieuTruocKhiNhap()
        {
            return ThamSo_DAO.THAMSO.LaySLTonToiThieuTruocKhiNhap();
        }

        public int LaySLTonToiThieuSauKhiBan()
        {
            return ThamSo_DAO.THAMSO.LaySLTonToiThieuSauKhiBan();
        }

        public float LaySLNoToiDa()
        {
            return ThamSo_DAO.THAMSO.LaySLNoToiDa();
        }

        public bool ApDung_QD_TienThuKhongVuotTienNo()
        {
            return ThamSo_DAO.THAMSO.ApDung_QD_TienThuKhongVuotTienNo();
        }
    }

    public class PhieuNhapSach_BUS
    {
        private static PhieuNhapSach_BUS _instance = null;

        public static PhieuNhapSach_BUS PHIEUNHAPSACH
        {
            get
            {
                if (_instance == null)
                    _instance = new PhieuNhapSach_BUS();

                return _instance;
            }
        }

        public bool ThemPhieuNhapSach(PhieuNhapSach_DTO p)
        {
            return PhieuNhapSach_DAO.PHIEUNHAPSACH.ThemPhieuNhapSach(p);
        }

        public void XoaPhieuNhapSach(int ma)
        {
            PhieuNhapSach_DAO.PHIEUNHAPSACH.XoaPhieuNhapSach(ma);
        }

        public int LayMaPhieuNhapLonNhat()
        {
            return PhieuNhapSach_DAO.PHIEUNHAPSACH.LayMaPhieuNhapLonNhat();
        }


    }

    public class ChiTietPhieuNhap_BUS
    {
        private static ChiTietPhieuNhap_BUS _instance = null;

        public static ChiTietPhieuNhap_BUS CHITIETPHIEUNHAP
        {
            get
            {
                if (_instance == null)
                    _instance = new ChiTietPhieuNhap_BUS();

                return _instance;
            }
        }

        public void ThemChiTietPhieuNhap(ChiTietPhieuNhap_DTO c)
        {
            ChiTietPhieuNhap_DAO.CHITIETPHIEUNHAP.ThemChiTietPhieuNhap(c);
        }

        public void XoaChiTietPhieuNhap(int ma)
        {
            ChiTietPhieuNhap_DAO.CHITIETPHIEUNHAP.XoaChiTietPhieuNhap(ma);
        }
    }

    public class HoaDon_BUS
    {
        private static HoaDon_BUS _instance = null;

        public static HoaDon_BUS HOADON
        {
            get
            {
                if (_instance == null)
                    _instance = new HoaDon_BUS();

                return _instance;
            }
        }

        public void ThemHoaDon(HoaDon_DTO h)
        {
            HoaDon_DAO.HOADON.ThemHoaDon(h);
        }

        public void XoaHoaDon(int ma)
        {
            HoaDon_DAO.HOADON.XoaHoaDon(ma);
        }

        public int LayMaHoDonLonNhat()
        {
            return HoaDon_DAO.HOADON.LayMaHoDonLonNhat();
        }
    }

    public class ChiTietHoaDon_BUS
    {
        private static ChiTietHoaDon_BUS _instance = null;

        public static ChiTietHoaDon_BUS CHITIETHOADON
        {
            get
            {
                if (_instance == null)
                    _instance = new ChiTietHoaDon_BUS();

                return _instance;
            }
        }

        public void ThemChiTietHoaDon(ChiTietHoaDon_DTO c)
        {
            ChiTietHoaDon_DAO.CHITIETHOADON.ThemChiTietHoaDon(c);
        }

        public void XoaChiTietHoaDon(int ma)
        {
            ChiTietHoaDon_DAO.CHITIETHOADON.XoaChiTietHoaDon(ma);
        }
    }

    public class KhachHang_BUS
    {
        private static KhachHang_BUS _instance = null;

        public static KhachHang_BUS KHACHHANG
        {
            get
            {
                if (_instance == null)
                    _instance = new KhachHang_BUS();

                return _instance;
            }
        }

        public void ThemKhachHang(KhachHang_DTO kh)
        {
            KhachHang_DAO.KHACHHANG.ThemKhachHang(kh);
        }

        public void SuaKhachHang(KhachHang_DTO kh)
        {
            KhachHang_DAO.KHACHHANG.SuaKhachHang(kh);
        }

        public void XoaKhachHang(int ma)
        {
            KhachHang_DAO.KHACHHANG.XoaKhachHang(ma);
        }

        public KhachHang_DTO LayKhachHang(int ma)
        {
            DataTable dt = KhachHang_DAO.KHACHHANG.LayKhachHang(ma);
            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return null;

            DataRow dr = list[0];

            string ten = dr["HoTen"].ToString();
            string dc = dr["DiaChi"].ToString();
            string em = dr["Email"].ToString();
            string dth = dr["DienThoai"].ToString();
            float tn = float.Parse(dr["TienNo"].ToString());

            return new KhachHang_DTO(ma, ten, dc, em, dth, tn);
        }
    }

    public class PhieuThuTien_BUS
    {
        private static PhieuThuTien_BUS _instance = null;

        public static PhieuThuTien_BUS PHIEUTHUTIEN
        {
            get
            {
                if (_instance == null)
                    _instance = new PhieuThuTien_BUS();

                return _instance;
            }
        }

        public void ThemPhieuThuTien(PhieuThuTien_DTO p)
        {
            PhieuThuTien_DAO.PHIEUTHUTIEN.ThemPhieuThuTien(p);
        }

        public void XoaPhieuThuTien(int ma)
        {
            PhieuThuTien_DAO.PHIEUTHUTIEN.XoaPhieuThuTien(ma);
        }

        public int LayMaPhieuThuLonNhat()
        {
            return PhieuThuTien_DAO.PHIEUTHUTIEN.LayMaPhieuThuLonNhat();
        }
    }

    public class BaoCaoTon_BUS
    {
        private static BaoCaoTon_BUS _instance = null;

        public static BaoCaoTon_BUS BAOCAOTON
        {
            get
            {
                if (_instance == null)
                    _instance = new BaoCaoTon_BUS();

                return _instance;
            }
        }

        public void ThemBaoCaoTon(BaoCaoTon_DTO p)
        {
            BaoCaoTon_DAO.BAOCAOTON.ThemBaoCaoTon(p);
        }

        public void XoaBaoCaoTon(int ma)
        {
            BaoCaoTon_DAO.BAOCAOTON.XoaBaoCaoTon(ma);
        }

        public DataTable TaoBaoCaoTon(string day)
        {
            return BaoCaoTon_DAO.BAOCAOTON.TaoBaoCaoTon(day);
        }
        public int LayMaBaoCaoTonLonNhat()
        {
            return BaoCaoTon_DAO.BAOCAOTON.LayMaBaoCaoTonLonNhat();
        }
    }

    public class ChiTietBaoCaoTon_BUS
    {
        private static ChiTietBaoCaoTon_BUS _instance = null;

        public static ChiTietBaoCaoTon_BUS CHITIETBAOCAOTON
        {
            get
            {
                if (_instance == null)
                    _instance = new ChiTietBaoCaoTon_BUS();

                return _instance;
            }
        }

        public void ThemChiTietBaoCaoTon(ChiTietBaoCaoTon_DTO p)
        {
            ChiTietBaoCaoTon_DAO.CHITIETBAOCAOTON.ThemChiTietBaoCaoTon(p);
        }

        public void XoaChiTietBaoCaoTon(int ma)
        {
            ChiTietBaoCaoTon_DAO.CHITIETBAOCAOTON.XoaChiTietBaoCaoTon(ma);
        }
    }

    public class BaoCaoCongNo_BUS
    {
        private static BaoCaoCongNo_BUS _instance = null;

        public static BaoCaoCongNo_BUS BAOCAOCONGNO
        {
            get
            {
                if (_instance == null)
                    _instance = new BaoCaoCongNo_BUS();

                return _instance;
            }
        }

        public void ThemBaoCaoCongNo(BaoCaoCongNo_DTO p)
        {
            BaoCaoCongNo_DAO.BAOCAOCONGNO.ThemBaoCaoCongNo(p);
        }

        public void XoaBaoCaoCongNo(int ma)
        {
            BaoCaoCongNo_DAO.BAOCAOCONGNO.XoaBaoCaoCongNo(ma);
        }

        public DataTable TaoBaoCaoCongNo(string day)
        {
            return BaoCaoCongNo_DAO.BAOCAOCONGNO.TaoBaoCaoCongNo(day);
        }

        public int LayMaBaoCaoCongNoLonNhat()
        {
            return BaoCaoCongNo_DAO.BAOCAOCONGNO.LayMaBaoCaoCongNoLonNhat();
        }
    }

    public class ChiTietBaoCaoCongNo_BUS
    {
        private static ChiTietBaoCaoCongNo_BUS _instance = null;

        public static ChiTietBaoCaoCongNo_BUS CHITIETBAOCAOCONGNO
        {
            get
            {
                if (_instance == null)
                    _instance = new ChiTietBaoCaoCongNo_BUS();

                return _instance;
            }
        }

        public void ThemChiTietBaoCaoCongNo(ChiTietBaoCaoCongNo_DTO p)
        {
            ChiTietBaoCaoCongNo_DAO.CHITIETBAOCAOCONGNO.ThemChiTietBaoCaoCongNo(p);
        }

        public void XoaChiTietBaoCaoCongNo(int ma)
        {
            ChiTietBaoCaoCongNo_DAO.CHITIETBAOCAOCONGNO.XoaChiTietBaoCaoCongNo(ma);
        }
    }

    public class NhanVien_BUS
    {
        private static NhanVien_BUS _instance = null;

        public static NhanVien_BUS NHANVIEN
        {
            get
            {
                if (_instance == null)
                    _instance = new NhanVien_BUS();

                return _instance;
            }
        }

        public void ThemNhanVien(NhanVien_DTO kh)
        {
            NhanVien_DAO.NHANVIEN.ThemNhanVien(kh);
        }

        public void SuaNhanVien(NhanVien_DTO kh)
        {
            NhanVien_DAO.NHANVIEN.SuaNhanVien(kh);
        }

        public void XoaNhanVien(int ma)
        {
            NhanVien_DAO.NHANVIEN.XoaNhanVien(ma);
        }

        public NhanVien_DTO LayNhanVien(int ma)
        {
            try
            {
                DataTable dt = NhanVien_DAO.NHANVIEN.LayNhanVien(ma);
                if (dt.Rows.Count == 0)
                    return null;
                DataRow dr = dt.Rows[0];
                string mt = (string)dr["MatKhau"];
                return new NhanVien_DTO((string)dr["HoTen"], mt, (int)dr["MaViTri"]);
            }
            catch
            {
                return null;
            }
        }
    }

    public class ViTri_BUS
    {
        private static ViTri_BUS _instance = null;

        public static ViTri_BUS VITRI
        {
            get
            {
                if (_instance == null)
                    _instance = new ViTri_BUS();

                return _instance;
            }
        }

        public void ThemViTri(ViTri_DTO v)
        {
            ViTri_DAO.VITRI.ThemViTri(v);
        }

        public void SuaViTri(ViTri_DTO v)
        {
            ViTri_DAO.VITRI.SuaViTri(v);
        }

        public void XoaViTri(int ma)
        {
            ViTri_DAO.VITRI.XoaViTri(ma);
        }

        public ViTri_DTO LayQuyenHan(int ma)
        {
            DataTable dt = ViTri_DAO.VITRI.LayQuyenHan(ma);
            if (dt.Rows.Count == 0)
                return null;
            DataRow dr = dt.Rows[0];
            string a = dr["TenViTri"].ToString();
            return new ViTri_DTO(ma, (string)dr["TenViTri"], Convert.ToBoolean(dr["NhapSach"]), Convert.ToBoolean(dr["LapHoaDon"]), Convert.ToBoolean(dr["TraCuuSach"]), Convert.ToBoolean(dr["LapPhieuThuTien"]), Convert.ToBoolean(dr["LapBaoCao"]), Convert.ToBoolean(dr["ThayDoiQuyDinh"]));
        }
    }
}
